// 参考文档：http://www.axios-js.com/zh-cn/docs/
import axios from 'axios';
import qs from 'qs';
import {message} from 'ant-design-vue';

import {getToken} from './sessionStorage'

//全局配置，告诉浏览器无论如何都要携带cookie去请求资源
axios.defaults.withCredentials=true

const service = axios.create({
	// timeout: 1000 * 3,
	withCredentials: true,
	paramsSerializer: function (params) {
		return qs.stringify(params, {arrayFormat: 'brackets'})
	},
	responseType: 'json', // default
	responseEncoding: 'utf8', // default
	headers: {
		'Content-Type': 'application/x-www-form-urlencoded',
		// 'Content-Type': 'application/json; charset=utf-8',
	}
});

service.interceptors.request.use(config => {
	if (config.method !== 'OPTION') {
		config.data = qs.stringify({
			...config.data
		})
		config.params = {
			...config.params
		}
		config.headers['token'] = getToken();
		return config;
	}
}, error => {
	console.log(error);
	return Promise.reject(error);
})

service.interceptors.response.use(response => {
	return response;
}, error => {
	console.log(error);
	message.error("系统错误，请联系管理员");
})

export default service;
